package com.zp.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {

    private static final String mailRegex = "^[\\w][\\w-\\.]+@[\\w-]+(\\.[\\w-]+)+$";
    private static final String phoneRegex = "^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";
    private static final String userRegex = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
    private static final String pswRegex = "^[a-zA-Z]\\w{5,17}$";
    private static final String snoRegex = "^20(\\d{14}|\\d{13})$";

    /**
     * 以数字或字母开头，允许下划线_横线-点. @ 域名
     */
    public static boolean checkMailRegex(String mail) {
        Pattern mailPattern = Pattern.compile(mailRegex);
        Matcher mailMatcher;
        mailMatcher = mailPattern.matcher(mail);
        return mailMatcher.matches();
    }

    /**
     * 电话号码 11位 以13、14、15、18开头
     */
    public static boolean checkPhoneRegex(String phone) {
        Pattern phonePattern = Pattern.compile(phoneRegex);
        Matcher phoneMatcher;
        phoneMatcher = phonePattern.matcher(phone);
        return phoneMatcher.matches();
    }

    /**
     * 用户名，字母开头，允许 5-16 字节，允许字母数字下划线
     */
    public static boolean checkUserRegex(String user) {
        Pattern userPattern = Pattern.compile(userRegex);
        Matcher userMatcher;
        userMatcher = userPattern.matcher(user);
        return userMatcher.matches();
    }

    /**
     * 密码，以字母开头，长度在 6~18 之间，只能包含字母、数字和下划线
     */
    public static boolean checkPswRegex(String psw) {
        Pattern pswPattern = Pattern.compile(pswRegex);
        Matcher pswMatcher;
        pswMatcher = pswPattern.matcher(psw);
        return pswMatcher.matches();
    }

    public static boolean checkSnoRegex(String sno) {
        Pattern snoPattern = Pattern.compile(snoRegex);
        Matcher snoMatcher;
        snoMatcher = snoPattern.matcher(sno);
        return snoMatcher.matches();
    }
}
